unit uFormAbout;

{$mode objfpc}{$H+}
{$i orcis.inc}

interface

uses
    Forms, Controls, Graphics, StdCtrls, Classes, Dialogs,
    ComCtrls, ButtonPanel, Grids,
    cef_tools,
    LCLVersion, ExtCtrls, LCLIntf,
    uInterfaces, uTypes;

type

    { TFrmAbout }

    TFrmAbout = class(TForm)
        ButtonPanel1: TButtonPanel;
        Image1: TImage;
        lblVerDetails: TLabel;
        lblCEFVersion: TLabel;
        lblName: TLabel;
        lblDownloads: TLabel;
        lblHomepage: TLabel;
        lblVerInfo: TLabel;
        pgcMain: TPageControl;
        stgVer: TStringGrid;
        TabSheet1: TTabSheet;
        TabSheet2: TTabSheet;
        tbsThirdparty: TTabSheet;
        procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
        procedure HelpButtonClick(Sender: TObject);
        procedure lblDownloadsClick(Sender: TObject);
        procedure lblDownloadsMouseEnter(Sender: TObject);
        procedure lblDownloadsMouseLeave(Sender: TObject);
        procedure lblHomepageClick(Sender: TObject);
    private
        { private declarations }
    protected
        Core: IISCore;

        procedure Init;
        procedure FillInfo;
    public
        { public declarations }
    end;

    { TLinkLabel }

    TLinkLabel = class(TLabel)
    private
        FLink: string;
        procedure SetLink(AValue: string);
    protected
        procedure Click; override;
        procedure MouseEnter; override;
        procedure MouseLeave; override;

        procedure IntMouseEnter;
        procedure IntMouseLeave;
        procedure IntMouseClick;
    public
        constructor Create(TheOwner: TComponent); override;
        property Link: string read FLink write SetLink;
    end;

procedure DoAbout;

implementation

procedure DoAbout;
begin
    with TFrmAbout.Create(Application) do
    begin
        Init;
        {$ifdef usemodal}
        try
            ShowModal;
        finally
            Free;
        end;
        {$else}
            Show;
        {$endif usemodal}
    end;
end;

{$R *.lfm}

{ TLinkLabel }

procedure TLinkLabel.SetLink(AValue: string);
begin
    if FLink = AValue then
        Exit;
    FLink := AValue;
end;

procedure TLinkLabel.Click;
begin
    inherited Click;
    IntMouseClick;
end;

procedure TLinkLabel.MouseEnter;
begin
    inherited MouseEnter;
    IntMouseEnter;
end;

procedure TLinkLabel.MouseLeave;
begin
    inherited MouseLeave;
    IntMouseLeave;
end;

procedure TLinkLabel.IntMouseEnter;
begin
    Font.Style := [fsUnderline];
end;

procedure TLinkLabel.IntMouseLeave;
begin
    Font.Style := [];
end;

procedure TLinkLabel.IntMouseClick;
begin
    OpenURL(FLink);
end;

constructor TLinkLabel.Create(TheOwner: TComponent);
begin
    inherited Create(TheOwner);
    Font.Color := clHotLight;
    Cursor := crHandPoint;
end;

{ TFrmAbout }

procedure TFrmAbout.lblDownloadsMouseEnter(Sender: TObject);
begin
    (Sender as TLabel).Font.Style := [fsUnderline];
end;

procedure TFrmAbout.lblDownloadsClick(Sender: TObject);
begin
    OpenURL('https://drive.google.com/folderview?id=0B0KjYHHcisnAQjQwYmI4V3pMQ1k');
end;

procedure TFrmAbout.HelpButtonClick(Sender: TObject);
begin
    ShowMessage('Does not implemented yet.');
end;

procedure TFrmAbout.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
    {$ifdef usemodal}
    CloseAction := caFree;
    {$endif usemodal}
end;

procedure TFrmAbout.lblDownloadsMouseLeave(Sender: TObject);
begin
    (Sender as TLabel).Font.Style := [];
end;

procedure TFrmAbout.lblHomepageClick(Sender: TObject);
begin
    OpenURL('https://code.google.com/p/orcis/');
end;

procedure TFrmAbout.Init;
begin
    Core := Application.MainForm as IISCore;

    Position := poMainFormCenter;
    {$ifndef usemodal}
    ShowInTaskBar := stAlways;
    {$endif usemodal}
    pgcMain.ActivePageIndex := 0;
    BorderStyle := bsDialog;
    Caption := 'About';
    lblVerInfo.Caption := 'Version: ' + orcisver;
    lblVerDetails.Caption := 'Compiled: ' + {$i %DATE%} + ' ' + {$i %TIME%} + ' for ' + {$i %FPCTARGETOS%};
    lblCEFVersion.Caption := 'CEF version: ' + GetCEFVersion;

    lblHomepage.Caption := 'Homepage';
    lblDownloads.Caption := 'Downloads';

    //CefLoadLibrary;

    FillInfo;
end;

procedure TFrmAbout.FillInfo;
const
    CThanksLines = 7;
var
    i: integer = 0;

    procedure SetVerInfo(const AItem: integer; const AName, AVer: string);
    begin
        stgVer.Cells[0, AItem] := AName;
        stgVer.Cells[1, AItem] := AVer;
    end;

    procedure AddThanksTo(const ACaption, ALink: string);
    var
        lbl: TLinkLabel;
    begin
        lbl := TLinkLabel.Create(Self);
        lbl.Caption := ACaption;
        lbl.Link := ALink;
        lbl.Parent := tbsThirdparty;
        lbl.Left := 8 + (i div CThanksLines) * (pgcMain.Width div 2);
        lbl.Top := 8 + (i mod CThanksLines) * 16;

        lbl.Hint := ALink;
        lbl.ShowHint := True;

        Inc(i);
    end;

begin
    stgVer.RowCount := 4;
    SetVerInfo(0, 'FreePascal', {$I %FPCVERSION%});
    SetVerInfo(1, 'Lazarus', lcl_version);
    SetVerInfo(2, 'CEF', GetCEFVersion);
    SetVerInfo(3, 'Chromium', GetChromiumVersion);

    AddThanksTo('Free Pascal', 'http://www.freepascal.org');
    AddThanksTo('Lazarus', 'http://www.lazarus.freepascal.org');
    AddThanksTo('CEF3', 'http://code.google.com/p/chromiumembedded/');
    AddThanksTo('fpCEF3', 'https://github.com/dliw/fpCEF3');
    AddThanksTo('WACEF', 'https://bitbucket.org/WaspAce/wacef');
    AddThanksTo('Synapse', 'http://wiki.lazarus.freepascal.org/Synapse');
    AddThanksTo('BGRABitmap', 'http://wiki.lazarus.freepascal.org/BGRABitmap');
    AddThanksTo('BGRAControls', 'http://wiki.lazarus.freepascal.org/BGRAControls');
    AddThanksTo('SQLite', 'http://www.sqlite.org/');
    AddThanksTo('Opera 12', 'http://en.wikipedia.org/wiki/Opera_(web_browser)#History');
    AddThanksTo('Farm-Fresh Web Icons', 'http://www.fatcow.com/free-icons');

    //AddThanksTo('', '');
    //AddThanksTo('', '');
    //AddThanksTo('', '');
    //AddThanksTo('', '');
    //AddThanksTo('', '');
end;

end.
